package cn.test.learnkotlin.v1

class BBaseType {
    /**
     * 十进制：123
     * 长整型以大写的L结尾：123L
     * 16进制以0x开头：0x0F
     * 2进制以0b开头：0b00011011
     * 不支持8进制
     * Double默认写法：123.5 123.5e10
     * Floats使用f或者F后缀：123.5f
     */

    private fun easyRecognize() {
        val oneMillion = 1_000_000
        val creditCardNumber = 1234_5678_9012_3456L
        val socialSecurityNumber = 999_99_9_9_9_9L
        val hexBytes = 0xFF_EC_DE_5E
        val bytes = 0b11010010_01101001_10010100_10010010
    }

    /**
     * 三个等号===表示比较对象地址，两个==表示比较大小
     */
    private fun fooCompare() {
        val a: Int = 1000
        val boxedA: Int? = a
        val boxedA1: Int? = a
        println(boxedA === boxedA1)
        println(boxedA == boxedA1)
    }

    private fun fooCast() {
        val b: Byte = 1
        //val i:Int  = b//错误
        val i: Int = b.toInt()

        val l: Long = 1L + 3 // Long + Int => Long

    }

    private fun fooBitTransform() {
        val a = 0b0001
        println("a:$a shl:${a shl 1}")
        val b = 0b0010
        println("b:$b shr:${b shr 1}")
        val c: Int = 0b0111_1111_1111_1111_1111_1111_1111_1111
        println("c:$c ushr:${c ushr 1}")
        val and1 = 0b0011
        val and2 = 0b0010
        println("and1:$and1, and2:$and2, and:${and1 and and2}")
        val or1 = 0b0001
        val or2 = 0b0010
        println("or1:$or1, or2:$or2, or:${or1 or or2}")
        or1 or or2
        val xor1 = 0b0000
        val xor2 = 0b0011
        println("xor1:$xor1, xor2:$xor2, xor:${xor1 xor xor2}")
        val inv1 = 0b0101
        val inv2 = 0b1010
        println("inv1:$inv1, inv:${inv1.inv()}, inv2:$inv2, inv:${inv2.inv()}")
    }

    private fun fooChar() {
        fun decimalDigitValue(c: Char): Int {
            if (c in '0'..'9') {
                return c.code - '0'.code
            } else {
                throw IllegalArgumentException("Out of range.")
            }
        }
        for (c in '0'..'9') {
            print("${decimalDigitValue(c)}")
        }
    }

    private fun fooBool() {
        val bool1 = true
        val bool2 = false
        print("||:${bool1 || bool2}, &&:${bool1 && bool2}, !bool2:${!bool2}, xor:${bool1 xor bool2}")
    }

    private fun fooArray() {
        val a = arrayOf(1, 2, 3, 4, 5)
        val b = Array(5, init = { i -> i })
        println("b2:${b[2]}")
        b[2] = 4
        print("b2:${b[2]}")
    }

    private fun fooStr() {
        val text1 = "A simple text."
        val text2 = """
        line1 
        line2 
        line3
        """
        val text3 = """
       line1 
       line2 
       line3
       """.trimIndent()
        println("text1:\n${text1}")
        println("text2:\n${text2}")
        println("text3:\n${text3}")
    }

    private fun fooStrPattern() {
        val s = "KK Up"
        val str = "$s.length is ${s.length}"
        println(str)
        print("Here is the $9.99 ")
    }

    private fun fooTest() {
        val a: Int = 127

//经过了装箱，创建了两个不同的对象
        val boxedA: Int? = a
        val anotherBoxedA: Int? = a

//虽然经过了装箱，但是值是相等的，都是100
        println(boxedA === anotherBoxedA) //  true，值相等，128 之前对象地址一样
        println(boxedA == anotherBoxedA) // true，值相等

        val c = 1000//不显示声明类型不会封装新对象
        val boxedC = c
        val anotherBoxedC = c
        println(boxedC === anotherBoxedC) //  true
        val d = 100//不显示声明类型不会封装新对象
        val boxedD = d
        val anotherBoxedD = d
        println(boxedD === anotherBoxedD) //  true
        println("type of a:${a.javaClass.name}, type of c:${c.javaClass.name}")
    }

    private fun fooTestPlus() {
        var boxedA: Int?
        var boxedB: Int?

// Int Int? Int?
        val a2: Int = 128
        boxedA = a2
        boxedB = a2
        log(" boxedA === boxedB ? " + (boxedA === boxedB))//false
//值相等
        log(" boxedA == boxedB ? " + (boxedA == boxedB))//true

// Int? Int? Int?
        val a3: Int? = 128
        boxedA = a3
        boxedB = a3
        log(" boxedA === boxedB ? " + (boxedA === boxedB))//true
//值相等
        log(" boxedA == boxedB ? " + (boxedA == boxedB))//true

// Int Int? Int?
        val a4: Int = 127
        boxedA = a4
        boxedB = a4
        log(" boxedA === boxedB ? " + (boxedA === boxedB))//true
//值相等
        log(" boxedA == boxedB ? " + (boxedA == boxedB))//true

// Int? Int? Int?
        val a5: Int? = 127
        boxedA = a5
        boxedB = a5
        log(" boxedA === boxedB ? " + (boxedA === boxedB))//true
//值相等
        log(" boxedA == boxedB ? " + (boxedA == boxedB))//true

// Int Int Int
        val a6: Int = 128
        var boxedA2: Int = a6
        var boxedB2: Int = a6
        log(" boxedA === boxedB ? " + (boxedA2 === boxedB2))//true
//值相等
        log(" boxedA == boxedB ? " + (boxedA2 == boxedB2))//true
// Int Int Int
        val a7: Int = 127
        boxedA2 = a7
        boxedB2 = a7
        log(" boxedA === boxedB ? " + (boxedA2 === boxedB2))//true
//值相等
        log(" boxedA == boxedB ? " + (boxedA2 == boxedB2))//true
    }

    private fun log(s: String) {
        println(s)
    }

    fun main() {
        fooTestPlus()
    }
}

fun main() {
    BBaseType().main()
}